package com.levi.mall.controller;

import com.github.pagehelper.PageInfo;
import com.levi.form.OrderCreateForm;
import com.levi.mall.service.OrderService;
import com.levi.pojo.User;
import com.levi.vo.OrderVo;
import com.levi.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author shentong
 * @since 2022/4/6 8:56 PM
 */
@RestController
@Api(tags = "订单接口")
@RequestMapping("/orders")
public class OrderController extends BaseController{

    @Autowired
    private OrderService orderService;

    @PostMapping
    @ApiOperation("创建订单")
    public ResponseVo<OrderVo> create(@ApiParam("地址id") @Valid @RequestBody OrderCreateForm form,
                                      HttpSession session) {
        User user = getCurrentUser(session);
        return orderService.create(user.getId(), form.getShippingId());
    }

    @GetMapping
    @ApiOperation("订单列表")
    public ResponseVo<PageInfo<OrderVo>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize,
            HttpSession session) {
        User user = getCurrentUser(session);
        return orderService.list(user.getId(), pageNum, pageSize);
    }

    @GetMapping("/{orderNo}")
    @ApiOperation("订单详情")
    public ResponseVo<OrderVo> detail(@PathVariable Long orderNo, HttpSession session) {
        User user = getCurrentUser(session);
        return orderService.detail(user.getId(), orderNo);
    }

    @PutMapping("/{orderNo}")
    @ApiOperation("取消订单")
    public ResponseVo cancel(@PathVariable Long orderNo, HttpSession session) {
        User user = getCurrentUser(session);
        return orderService.cancel(user.getId(), orderNo);
    }
}
